load(
    "//pkg/util/log/eventpb:PROTOS.bzl",
    _EVENTPB_PROTOS = "EVENTPB_PROTOS",
    _EVENTPB_PROTO_DEPS = "EVENTPB_PROTO_DEPS",
    _EVENTPB_PROTO_LOCATIONS = "EVENTPB_PROTO_LOCATIONS",
)
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

exports_files(_EVENTPB_PROTOS)

go_library(
    name = "eventpb",
    srcs = [
        "doc.go",
        "events.go",
        "sql_audit_events.go",
        ":gen-eventlog-channels-generated-go",  # keep
        ":gen-json-encode-generated-go",  # keep
    ],
    embed = [":eventpb_go_proto"],
    importpath = "github.com/cockroachdb/cockroach/pkg/util/log/eventpb",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/sql/appstatspb",  # keep
        "//pkg/util/jsonbytes",  # keep
        "//pkg/util/log/logpb",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_redact//:redact",
        "@com_github_gogo_protobuf//jsonpb",  # keep
    ],
)

go_test(
    name = "eventpb_test",
    size = "small",
    srcs = ["event_test.go"],
    embed = [":eventpb"],
    deps = [
        "//pkg/util/log/logpb",
        "@com_github_cockroachdb_redact//:redact",
        "@com_github_stretchr_testify//assert",
    ],
)

proto_library(
    name = "eventpb_proto",
    srcs = [
        "cluster_events.proto",
        "ddl_events.proto",
        "debug_events.proto",
        "events.proto",
        "health_events.proto",
        "job_events.proto",
        "misc_sql_events.proto",
        "privilege_events.proto",
        "role_events.proto",
        "session_events.proto",
        "sql_audit_events.proto",
        "storage_events.proto",
        "telemetry.proto",
        "zone_events.proto",
    ],
    strip_import_prefix = "/pkg",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/sql/catalog/descpb:descpb_proto",
        "//pkg/util/log/logpb:logpb_proto",
        "@com_github_gogo_protobuf//gogoproto:gogo_proto",
    ],
)

go_proto_library(
    name = "eventpb_go_proto",
    compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
    importpath = "github.com/cockroachdb/cockroach/pkg/util/log/eventpb",
    proto = ":eventpb_proto",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/sql/catalog/descpb",
        "//pkg/util/log/logpb",
        "@com_github_gogo_protobuf//gogoproto",
    ],
)

genrule(
    name = "gen-eventlog-channels-generated-go",
    srcs = _EVENTPB_PROTO_DEPS,
    outs = ["eventlog_channels_generated.go"],
    cmd = """
    $(location //pkg/util/log/eventpb/eventpbgen:eventpbgen) eventlog_channels_go \
        {} \
        >$(location eventlog_channels_generated.go)
    """.format(_EVENTPB_PROTO_LOCATIONS),
    tools = [
        "//pkg/util/log/eventpb/eventpbgen",
    ],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

genrule(
    name = "gen-json-encode-generated-go",
    srcs = _EVENTPB_PROTO_DEPS,
    outs = ["json_encode_generated.go"],
    cmd = """
    $(location //pkg/util/log/eventpb/eventpbgen:eventpbgen) \
        --excluded-events='CommonEventDetails' \
        json_encode_go \
        {} \
        >$(location json_encode_generated.go)
    """.format(_EVENTPB_PROTO_LOCATIONS),
    tools = [
        "//pkg/util/log/eventpb/eventpbgen",
    ],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)
